#include "siteLogic.h"

int master_site_start() {
// 增加log打印
    string path = "/data/changhong/edge_midware/lhy/masterlog.txt";
    muduo::logInitLogger(path);

    LOG_RED << "-----------------------------------------";
    LOG_RED << "-----------------------------------------";
    LOG_RED << "---------------MASTER START-------------";
    LOG_RED << "-----------------------------------------";
    LOG_RED << "-----------------------------------------";

    httplib::ThreadPool threadPool_(10);
    ServiceSiteManager* serviceSiteManager = ServiceSiteManager::getInstance();
    serviceSiteManager->setServerPort(SITE_PORT_MASTER);
    serviceSiteManager->setSiteIdSummary(SITE_ID_MASTER, SITE_NAME_MASTER);

    
// 注册服务
    // get_master
    serviceSiteManager->registerServiceRequestHandler(SERVICE_ID_GET_MASTER,
                                                      [](const Request& request, Response& response)->int{
        return get_master_service_handler(request, response);
    });

    // get_all
    serviceSiteManager->registerServiceRequestHandler(SERVICE_ID_GET_ALL,
                                                      [](const Request& request, Response& response)->int{
        return get_all_service_handler(request, response);
    });

    // get_app_site_list
    serviceSiteManager->registerServiceRequestHandler(SERVICE_ID_GET_APP_SITE_LIST,
                                                      [](const Request& request, Response& response)->int{
        return get_app_site_listr_service_handler(request, response);
    });
    

// 声明消息
    serviceSiteManager->registerMessageId(MESSAGE_ID_MASTER_UPDATE);                
    serviceSiteManager->registerMessageId(MESSAGE_ID_ALL_UPDATE);
    serviceSiteManager->registerMessageId(MESSAGE_ID_APP_SITE_LIST_UPDATE);
    
// 注册消息处理函数
    serviceSiteManager->registerMessageHandler(MESSAGE_ID_MASTER_UPDATE,        SITE_ID_MASTER,     master_update_messageHandle);
    serviceSiteManager->registerMessageHandler(MESSAGE_ID_ALL_UPDATE,           SITE_ID_MASTER,     all_update_messageHandle);
    serviceSiteManager->registerMessageHandler(MESSAGE_ID_APP_SITE_LIST_UPDATE, SITE_ID_MASTER,     app_site_list_update_messageHandle);
    serviceSiteManager->registerMessageHandler(MESSAGE_ID_MASTER_SLAVE_SWITCH,  SITE_ID_HONGMEI,    master_slave_switch_messageHandle);
    
    
// 站点监听线程启动
    threadPool_.enqueue([&](){
        while(true){
            //自启动方式
            int code = serviceSiteManager->start();
            //注册启动方式
            // int code = serviceSiteManager->startByRegister();

            if(code != 0){
                std::cout << "===>master startByRegister error, code = " << code << std::endl;
                std::cout << "===>master startByRegister in 3 seconds...." << std::endl;
                std::this_thread::sleep_for(std::chrono::seconds(3));
            }else{
                std::cout << "===>synergySite startByRegister successfully....." << std::endl;
                break;
            }
        }
    });


    return 0;
}
















